package com.xiaoshangkou.smalltv2.ui.gallery;

import static android.content.Context.MEDIA_PROJECTION_SERVICE;

import android.app.Activity;
import android.content.Intent;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.xiaoshangkou.smalltv2.databinding.FragmentGalleryBinding;
import com.xiaoshangkou.smalltv2.recording.ScreenRecordService;
import com.xiaoshangkou.smalltv2.thread.SubThread_TT2;

public class GalleryFragment extends Fragment {

    private GalleryViewModel galleryViewModel;
    private FragmentGalleryBinding binding;
    private static final int REQUEST_CODE = 1;
    private Thread thread;
    private MediaProjectionManager mMediaProjectionManager;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        galleryViewModel =
                new ViewModelProvider(this).get(GalleryViewModel.class);

        binding = FragmentGalleryBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        mMediaProjectionManager = (MediaProjectionManager)getActivity().getSystemService(MEDIA_PROJECTION_SERVICE);
        Button videobutton1 = binding.videobutton1;
        galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                videobutton1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EditText editText = binding.editText1;
                        String ip=editText.getText().toString();
                        thread = new Thread(new SubThread_TT2(ip));
                        thread.start();
                        Intent captureIntent = mMediaProjectionManager.createScreenCaptureIntent();
                        startActivityForResult(captureIntent, REQUEST_CODE);

                    }
                });
            }
        });
        return root;
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //如果权限满足
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            try {
                DisplayMetrics metric = new DisplayMetrics();
                getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(metric);

                int width = metric.widthPixels; // 宽度（PX）
                int height = metric.heightPixels; // 高度（PX）
                float density = metric.density; // 密度（0.75 / 1.0 / 1.5）
                int densityDpi = metric.densityDpi;
                Toast.makeText(getActivity(), "允许录屏", Toast.LENGTH_SHORT).show();

                Intent service = new Intent(getActivity(), ScreenRecordService.class);
                service.putExtra("resultCode", resultCode);
                service.putExtra("data", data);
                service.putExtra("width", width);
                service.putExtra("densityDpi", densityDpi);
                service.putExtra("height", height);
                service.setAction("android.intent.action.RESPOND_VIA_MESSAGE");
                getActivity().startService(service);

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(getActivity(), "拒绝录屏", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}